package com.luyuan.crm.product.web;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.model.DataModel;

import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.product.model.ProductType;
import com.luyuan.crm.product.service.ProductService;
import com.luyuan.crm.product.service.ProductTypeService;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;
import com.ruijie.foundation.exception.BusinessException;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;

@Scope("view")
@Component("productTypeAction")
public class ProductTypeAction extends BaseAction{
	
	@Resource(name = "productTypeService")
	private ProductTypeService productTypeService;
	
	@Autowired
	private ProductService productService;
	
	private ProductType criteria = new ProductType();
	
	private ProductType productType;

	public DataModel getTableModel() {
        TablePage limit = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<Order> orders = new ArrayList<Order>();
        for (TableOrder tableOrder : tableOrders) {
        	Order order = tableOrder.toOrder();
            if (order != null) {
                orders.add(order);
            }
        }
        orders.add(Order.desc("id"));
        CriteriaTemplate c = CriteriaTemplate.forClass(ProductType.class);
        RestrictionTemplate r = new RestrictionTemplate(true);
        c.add(r.like("typeName", criteria.getTypeName()));
        Page page = this.productTypeService.pagedQuery(c, limit.getPage(), limit.getPageSize(), orders);
        return new PageDataModel(page);
    }
	
	public ProductType getCriteria() {
		return criteria;
	}
	
	public ProductType getProductType() {
		String productTypeId = this.getRequestParameter("productTypeId");
		if (productTypeId != null && !"".equals(productTypeId)) {
            this.productType = this.productTypeService.getProductTypeById(productTypeId);
        }
        if (productType == null) {
        	productType = new ProductType();
        }
		return productType;
	}
	
	public void addProductType(){
		this.productTypeService.addProductTypeModel(productType);
		this.addGlobalI18nMessage("添加产品类别信息成功");
	}
	
	public void updateProductType(){
		this.productTypeService.updateProductType(productType);
		this.addGlobalI18nMessage("修改产品类别信息成功");
	}
	
	public void deleteByProductId(String productTypeId){
		if (productTypeId == null && "".equals(productTypeId)) {
			this.addGlobalI18nMessage(new BusinessException("产品类别ID不能为空"));
			return;
		}
		if(productService.hasChildProducts(productTypeId)){
			this.addGlobalI18nMessage(new BusinessException("请先删除该类别下的所有产品"));
			return;
		}
		this.productTypeService.deleteProductTypeById(productTypeId);
		this.addGlobalI18nMessage("删除成功");
	}
	
	public void batchDelete(){
		String[] selectedIds = getRequestParameterValues("id");
        if (selectedIds == null){
        	selectedIds = new String[0];
        	this.addGlobalI18nMessage("请至少选择一条数据");
        	return;
        }
        
        for(String id : selectedIds){
        	if(productService.hasChildProducts(id)){
    			this.addGlobalI18nMessage(new BusinessException("请先删除类别下的所有产品"));
    			return;
    		}
        }
        
        this.productTypeService.batchDeleteProductType((Serializable[])selectedIds);
        this.addGlobalI18nMessage("批量删除成功");
	}
}
